from rest_framework.views import exception_handler
from django.db import DatabaseError
from rest_framework import status
from rest_framework.response import Response
#导入日志 实例化日志对象
import logging
logger = logging.getLogger("django")
#自定义报错方法
def custom_excrption_handler(exc,context):
    #报错方法
    #django自带的错误识别
    response = exception_handler(exc,context)
    #自定义错误识别
    if response is None:
        view = context["view"]
        #数据库错误是被
        if isinstance(exc,DatabaseError):

            logger.error("数据库发生异常。view=%s，exc=%s" % (view, exc))
            response = Response({"message":"服务器内部错误"})
        #未知错误识别
        if isinstance(exc,Exception):

            logger.error("位置异常 view=%s，exc=%s" % (view, exc))
            response = Response({"message":"服务器错误"})

    return response